////////////////////////////////////////////////////////////////////////////////
/// @file    mqttclient.h
/// @author  AE TEAM
/// @brief   THIS FILE PROVIDES ALL THE SYSTEM FIRMWARE FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

#ifndef __MQTTCLIENT_H
#define __MQTTCLIENT_H
#include "mm32_device.h"
#include "lwipopts.h"

////////////////////////////////////////////////////////////////////////////////
/// @defgroup MM32_Example_Layer
/// @brief MM32 Example Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @defgroup MM32_RESOURCE
/// @brief MM32 Examples resource modules
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @defgroup MM32_Exported_Constants
/// @{

#define   MSG_MAX_LEN     500
#define   MSG_TOPIC_LEN   50
#define   KEEPLIVE_TIME   50
#define   MQTT_VERSION    4

#ifdef    LWIP_DNS
#define   HOST_NAME       "rvmbdy8.mqtt.iot.gz.baidubce.com"
#else
#define   HOST_NAME       "14.215.190.20"
#endif


//#define   HOST_IP       "129.204.201.235"
#define   HOST_PORT     1883

#define   CLIENT_ID     "client_ID"
#define   USER_NAME     "rw41x0s/mm32"
#define   PASSWORD      "P0IQRETB86Th3lkW"

#define   TOPIC         "temp&hum"

#define   TEST_MESSAGE  "test_message"
/// @}

////////////////////////////////////////////////////////////////////////////////
/// @defgroup MM32_Exported_Enumeration
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @brief XXXX enumerate definition.
/// @anchor XXXX
////////////////////////////////////////////////////////////////////////////////

typedef struct {
    uint8_t  humi_high8bit;
    uint8_t  humi_low8bit;
    uint8_t  temp_high8bit;
    uint8_t  temp_low8bit;
    uint8_t  check_sum;
    double    humidity;
    double    temperature;
} DHT11_Data_TypeDef;

enum QoS {
    QOS0 = 0,
    QOS1,
    QOS2
};

enum MQTT_Connect {
    Connect_OK = 0,
    Connect_NOK,
    Connect_NOTACK
};


typedef struct __MQTTMessage {
    uint32_t qos;
    uint8_t retained;
    uint8_t dup;
    uint16_t id;
    uint8_t type;
    void* payload;
    int32_t payloadlen;
} MQTTMessage;


typedef struct __MQTT_MSG {
    uint8_t  msgqos;
    uint8_t  msg[MSG_MAX_LEN];
    uint32_t msglenth;
    uint8_t  topic[MSG_TOPIC_LEN];
    uint16_t packetid;
    uint8_t  valid;
} MQTT_USER_MSG;

typedef struct {
    int8_t topic[MSG_TOPIC_LEN];
    int8_t qos;
    int8_t retained;

    uint8_t msg[MSG_MAX_LEN];
    uint8_t msglen;
} mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t;



/// @}

////////////////////////////////////////////////////////////////////////////////
/// @defgroup MM32_Exported_Variables
/// @{
#ifdef _MQTTCLIENT_C_
#define GLOBAL

#else
#define GLOBAL extern







#endif





#undef GLOBAL

/// @}


////////////////////////////////////////////////////////////////////////////////
/// @defgroup MM32_Exported_Functions
/// @{





void mqtt_thread( void* pvParameters);


int32_t MQTT_PingReq(int32_t sock);


uint8_t MQTT_Connect(void);


int32_t MQTTSubscribe(int32_t sock, char* topic, enum QoS pos);


void UserMsgCtl(MQTT_USER_MSG*  msg);


uint16_t GetNextPackID(void);


//int32_t MQTTMsgPublish(int32_t sock, char *topic, int8_t qos, int8_t retained,uint8_t* msg,uint32_t msg_len);
int32_t MQTTMsgPublish(int32_t sock, char* topic, int8_t qos, uint8_t* msg);

int32_t ReadPacketTimeout(int32_t sock, uint8_t* buf, int32_t buflen, uint32_t timeout);


void mqtt_pktype_ctl(uint8_t packtype, uint8_t* buf, uint32_t buflen);

int32_t WaitForPacket(int32_t sock, uint8_t packettype, uint8_t times);


void
mqtt_thread_init(void);

/// @}


/// @}

/// @}


////////////////////////////////////////////////////////////////////////////////
#endif
////////////////////////////////////////////////////////////////////////////////



